package cn.yshujia.controller;

import cn.yshujia.domain.dto.ArticleDTO;
import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.ArchiveVO;
import cn.yshujia.domain.vo.ArticleVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.enums.MinioFolder;
import cn.yshujia.service.impl.ArticleServiceImpl;
import cn.yshujia.utils.MinioUtils;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 文章控制器
 */

@RestController
@Tag (name = "Article", description = "文章Api")
public class ArticleController {
	
	@Resource
	private ArticleServiceImpl service;
	
	@PostMapping ("/article/upload/files")
	@Operation (summary = "上传文章图片，返回预览地址")
	public ApiVO<List<String>> upload(@RequestParam ("files") MultipartFile[] files) {
		List<String> urls = MinioUtils.upload(files, MinioFolder.ARTICLE);
		if (ObjectUtils.isEmpty(urls)) {
			return ApiVO.error();
		}
		urls.replaceAll(s -> MinioUtils.STATIC_DOMAIN + s);
		return ApiVO.success(urls);
	}
	
	
	@GetMapping ("/article/{id}")
	@Operation (summary = "根据 文章ID 获取文章")
	public ApiVO<ArticleVO> article(@PathVariable Long id) {
		return ApiVO.success(service.selectArticle(id));
	}
	
	@GetMapping ("/article/page")
	@Operation (summary = "通过 标签ID 为参数数据获取文章分页集合")
	public ApiVO<PageVO<ArticleVO>> list(@RequestParam ("dto") String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.pageByLabelId(dtoPage));
	}
	
	@GetMapping ("/article/deploy/list")
	@Operation (summary = "获取部署文章列表")
	public ApiVO<List<ArticleVO>> deployList() {
		return ApiVO.success(service.selectDeployList());
	}
	
	@GetMapping ("/article/recommend/list")
	@Operation (summary = "获取推荐文章列表")
	public ApiVO<List<ArticleVO>> recommend() {
		return ApiVO.success(service.selectRecommendList());
	}
	
	@GetMapping ("/article/archive/list")
	@Operation (summary = "获取所有文章归档信息")
	public ApiVO<List<ArchiveVO>> archiveList() {
		return ApiVO.success(service.selectArchiveList());
	}
	
	@GetMapping ("/admin/article/page")
	@Operation (summary = "admin 通过分页数据获取文章集合")
	@PreAuthorize ("@sys.hasOnePermission('article:all')")
	public ApiVO<PageVO<ArticleVO>> adminList(@RequestParam ("dto") String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(dtoPage));
	}
	
	
	@PostMapping ("/admin/article/insert")
	@Operation (summary = "admin 插入文章")
	@PreAuthorize ("@sys.hasOnePermission('article:all')")
	public ApiVO<Boolean> insert(@RequestBody ArticleDTO dto) {
		boolean bool = service.insert(dto.getArticle(), dto.getUrls());
		if (bool) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/admin/article/update")
	@Operation (summary = "admin 更新文章，urls 中存放新上传的所有文件的地址")
	@PreAuthorize ("@sys.hasOnePermission('article:all')")
	public ApiVO<Boolean> update(@RequestBody ArticleDTO dto) {
		boolean result = service.update(dto.getArticle(), dto.getUrls());
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}